﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sylvain.Manager;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using Sylvain.Model;

public partial class Employees : System.Web.UI.Page
{
    //Instantiation of SylvainManager Class.
    ISylvainManager objManager = new SylvainManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[AppSession.CurrentUser.ToString()] != null)
        {
            Response.Redirect("~/EmployeeLoggedIn.aspx");
        }
        if(IsPostBack)
        {
            divNewAccount.Style.Add("display", "block");
        }
    }
    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Session[AppSession.CurrentUser.ToString()] = objManager.GetLogin(this.txtUsernameLogin.Text, this.txtPassword.Text);
            Response.Redirect("~/EmployeeLoggedIn.aspx");
        }
        catch (Exception ex)
        {
            if (ex.Message.ToLower().Contains("invalid"))
            {
                this.ShowAlert(ex.Message);
            }
            else
            {
                this.ShowAlert("There is some error. Please try again.");
            }
            this.txtUsernameLogin.Text = "";
            this.txtPassword.Text = "";
        }        
    }
    protected void btnSubmitForgotPassword_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            objManager.ForgotPassword(this.txtEmailName.Text, this.txtUserNameForgotPassword.Text, System.Configuration.ConfigurationManager.AppSettings["UserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Password"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Host"].ToString());
            this.ShowAlert("Your password has been sent to your e-mail address.");
            this.txtUserNameForgotPassword.Text = "";
            this.txtEmailName.Text = "";
        }
        catch(Exception ex)
        {
            if (ex.Message.ToLower().Contains("exists"))
            {
                this.ShowAlert(ex.Message);
            }
            else
            {
                this.ShowAlert("There is some error. Please try again.");
            }
            this.txtUserNameForgotPassword.Text = "";
            this.txtEmailName.Text = "";
        }
    }

    private void ShowAlert(string message)
    {
        // Cleans the message to allow single quotation marks 
        string cleanMessage = message.Replace("'", "\'");
        string script = @"<script type='text/javascript'>setTimeout(""alert('" + cleanMessage + "')";
        script +=@""",500);</script>";

        // Gets the executing web page 
        Page page = HttpContext.Current.CurrentHandler as Page;

        // Checks if the handler is a Page and that the script isn't allready on the Page 
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script);
        } 
    }
    protected void btnNewAccount_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Employee objEmployee = new Employee();
            objEmployee.FirstName = this.txtFirstName.Text.Trim();
            objEmployee.LastName = this.txtLastName.Text.Trim();
            objEmployee.UserName = this.txtUserNameNewAccount.Text.Trim();
            objEmployee.Password = this.txtPasswordNewAccount.Text.Trim();
            objEmployee.Email = this.txtEmail.Text.Trim();
            objEmployee.Approved = false;
            objEmployee.CreatedDate = System.DateTime.Now;
            objEmployee.UpdatedDate = System.DateTime.Now;
            objManager.AddEmployee(objEmployee, null, null, null);
            this.ShowAlert("Your account has been successfully submitted for approval. You will be notified by e-mail.");
            this.txtFirstName.Text = "";
            this.txtLastName.Text = "";
            this.txtPasswordNewAccount.Text = "";
            this.txtUserNameNewAccount.Text = "";
            this.txtEmail.Text = "";
            this.txtPasswordConfirm.Text = "";
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("already exists"))
            {
                this.ShowAlert(ex.Message);
            }
            else
            {
                this.ShowAlert("There is some error. please try again.");
            }
        }
    }
}